﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PalFramework.Manager;
using PalFramework.Template;
using PalBattleEmulator.Template.Tools;
using System.IO;

namespace PalBattleEmulator.Manager.Tempate
{
    public abstract class PalObjectTmplManager<T> : IPalObjectTmplManager<T>
    {
        protected string excelFileName;
        protected string excelSheetName;
        protected string xmlFileName;
        protected string xmlRootName;
        protected Dictionary<int, T> templates = new Dictionary<int,T>();

        public PalObjectTmplManager(string excelFileName, string excelSheetName, string xmlFileName, string xmlRootName)
        {
            this.excelFileName = excelFileName;
            this.excelSheetName = excelSheetName;
            this.xmlFileName = xmlFileName;
            this.xmlRootName = xmlRootName;
        }


        protected List<Dictionary<string, string>> loadFromExcel()
        {
            string path = Path.Combine(GlobalConfig.PATH_DATA, GlobalConfig.DIR_NAME_DATA_EXCEL);
            string fileName = excelFileName + "." + GlobalConfig.FILE_FORMAT_DATA_EXCEL;
            path = Path.Combine(path, fileName);
            List<Dictionary<string, string>> data = ExcelTemplateTools.LoadExcelFile(path, excelSheetName);
            return data;
            
        }

        protected List<Dictionary<string, string>> loadFromXml()
        {
            string path = Path.Combine(GlobalConfig.PATH_DATA, GlobalConfig.DIR_NAME_DATA_XML);
            string fileName = xmlFileName + "." + GlobalConfig.FILE_FORMAT_DATA_XML;
            path = Path.Combine(path, fileName);
            List<Dictionary<string, string>> data = XmlTemplateTools.LoadXmlFile(path, xmlRootName, GlobalConfig.DATA_XML_NODE);
            return data;
        }

        #region IPalObjectTmplManager<T> 成员

        public T GetTmpl(int tmplId)
        {
            if(templates.ContainsKey(tmplId))
                return templates[tmplId];
            return default(T);
        }

        #endregion

        #region IManager 成员

        public abstract void Initialize();

        #endregion
    }
}
